package com.enniswu.weitter.base;

import android.text.TextUtils;

import com.enniswu.weitter.data.local.SPSupport;
import com.enniswu.weitter.network.HttpClient;

public abstract class BasePresenter<V extends BaseContract.View> implements BaseContract.Presenter {

    protected V view;

    public BasePresenter(V view) {
        this.view = view;
        this.view.setPresenter(this);
    }

    @Override
    public void detachView() {
        view = null;
    }

    protected boolean handleNotLog() {
        if (!TextUtils.isEmpty(SPSupport.getInstance().getSessionId())) {
            return false;
        }
        view.showAlertLogin();
        return true;
    }

    public boolean handleNoNullSuccess(Object o) {
        if (view == null)
            return false;
        if (o == null) {
            view.showMessage(HttpClient.getErrorMessage());
            view.dismissLoading();
            return false;
        }
        return true;
    }

    public abstract class AbstractCallback<T> implements BaseModel.Callback<T> {
        @Override
        public void onFailure(String msg) {
            if (view == null)
                return;
            view.dismissLoading();
            view.showMessage(msg);
        }
    }
}
